package com.netdisk.service.impl;

import com.netdisk.dto.UserActivityDTO;
import com.netdisk.entity.File;
import com.netdisk.entity.UserActivity;
import com.netdisk.mapper.UserMapper;
import com.netdisk.service.UserActivityService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserActivityServiceImpl implements UserActivityService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 插入基本用户活动记录
     * @param file
     * @param description
     * @param actionType
     * @return
     */
    @Override
    public void insertActivity(File file, String description,String actionType) {
        UserActivityDTO userActivityDTO = new UserActivityDTO();
        userActivityDTO.setUserId(file.getUserId());
        userActivityDTO.setFileId(file.getId());
        userActivityDTO.setFileName(file.getName());
        userActivityDTO.setActionType(actionType);
        userActivityDTO.setDescription(description);
        userMapper.insertActivity(userActivityDTO);
    }

    /**
     * 插入完整的参数的用户活动记录
     * @param file
     * @param description
     * @param actionType
     * @param userAgent
     * @param ipAddress
     * @return
     */
    public UserActivityDTO insertActivity(File file, String description,String actionType,String userAgent,String ipAddress) {
        UserActivityDTO userActivityDTO = new UserActivityDTO();
        userActivityDTO.setUserId(file.getUserId());
        userActivityDTO.setFileId(file.getId());
        userActivityDTO.setFileName(file.getName());
        userActivityDTO.setActionType(actionType);
        userActivityDTO.setDescription(description);
        userActivityDTO.setUserAgent(userAgent);
        userActivityDTO.setIpAddress(ipAddress);
        userMapper.insertActivity(userActivityDTO);
        return userActivityDTO;
    }

    /**
     * 插入用户活动记录，带IP地址
     * @param file
     * @param description
     * @param actionType
     * @param ipAddress
     * @return
     */
    @Override
    public void insertActivityWithIpAddress(File file ,String description,String actionType,String ipAddress) {
        insertActivity(file, description, actionType, null, ipAddress);
    }

    /**
     * 插入用户活动记录，带用户代理
     * @param file
     * @param description
     * @param actionType
     * @param userAgent
     * @return
     */
    @Override
    public void insertActivityWithUserAgent(File file ,String description,String actionType,String userAgent) {
        insertActivity(file, description, actionType, userAgent, null);
    }

    public List<UserActivityDTO> getActivityListById(Integer userId) {
        List<UserActivity> activityList = userMapper.getActivityList(userId);
        System.out.println(activityList);
        List<UserActivityDTO> activityDTOList = new ArrayList<>();
        for (UserActivity userActivity : activityList){
            UserActivityDTO userActivityDTO = new UserActivityDTO();
            BeanUtils.copyProperties(userActivity,userActivityDTO);
            activityDTOList.add(userActivityDTO);
        }
        return activityDTOList;
    }

}
